package com.zh.common.net.exception

import java.lang.RuntimeException
import java.util.*

/**
 * API接口访问异常
 */
class ApiException(private val code: Int, message: String) : RuntimeException(message) {

    private val apiCode = mutableMapOf(
        Pair(401, "未登录"),
    )

    fun getApiError(): Pair<Int, String?> {
        errorCode(code)
        return Pair(code, apiCode[code] ?: message)
    }

    /**
     * 异常状态全局通知
     */
    private fun errorCode(code: Int) {
        ServiceLoader.load(ServerErrorNotifyInterface::class.java).forEach { it.errorNotify(code) }
    }
}